package csc.daonjpa.java.controller;

import java.util.List;

import javax.persistence.PersistenceException;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.hibernate.exception.ConstraintViolationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import csc.daonjpa.java.domain.Customer;
import csc.daonjpa.java.service.CustomerService;
@Controller
public class CustomerController {
	private static final Logger LOG = Logger.getLogger(AccountController.class);
	private CustomerService customerService;
	
	public CustomerService getCustomerService() {
		return customerService;
	}
	@Autowired
	public void setCustomerService(CustomerService customerService) {
		this.customerService = customerService;
	}

	/**
	 * Adds account to DB
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/createCustomer")
	public String addAccount(HttpServletRequest request, Model model) {
		// Read account info from request and save into Account object
		String firstName = request.getParameter("firstName");
		String lastName = request.getParameter("lastName");
		String address = request.getParameter("address");
		String phone = request.getParameter("phone");
		String loginId = request.getParameter("loginId");
		String password = request.getParameter("password");
	
		Customer customer = new Customer ( loginId, password, firstName, lastName,
				address, phone);
		try {
			// Save account to DB
			customerService.addCustomer(customer);	
			model.addAttribute("successMessage",
					"Added new customer successfully");
		} catch (PersistenceException ex) {
			if (ex.getCause() instanceof ConstraintViolationException) {
				model.addAttribute("errorMessage", "Duplicated account found");
			} else {
				model.addAttribute("errorMessage", "Error adding new customer");
			}
			LOG.error("Error found adding an customer", ex);
			throw ex;
		} catch (Exception e) {
			LOG.error("General Error found adding an customer", e);
			throw e;
		}
		
		return "forward:/listCustomer";
	}

	@RequestMapping(value = "/listCustomer")
	public ModelAndView getCustomerList(Model model) {

		// Get the list of all accounts from DB
		List<Customer> customerList = customerService.getCustomers();
		ModelAndView modelAndView = new ModelAndView("customerList");
		modelAndView.addObject("listCustomers",customerList );
		return modelAndView;
	}
	
	@ExceptionHandler(Exception.class)
	public ModelAndView handleAllException(Exception ex) {
 
		ModelAndView model = new ModelAndView("customerList");
		model.addObject("errorMessage", "Exception occurred");
 
		return model;
	}
}
